package com.zhiche.lisa.bms.service.receipt.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zhiche.lisa.bms.dao.mapper.receipt.ReceiptAffirmDtlMapper;
import com.zhiche.lisa.bms.dao.model.invoice.InvoiceReg;
import com.zhiche.lisa.bms.dao.model.receipt.ReceiptAffirmDtl;
import com.zhiche.lisa.bms.pojo.vo.receipt.ReceiptAffirmDtlAndInvoiceVO;
import com.zhiche.lisa.bms.service.invoice.InvoiceRegService;
import com.zhiche.lisa.bms.service.receipt.ReceiptAffirmDtlService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 收款确认明细表 服务实现类
 * </p>
 *
 * @author hongbo
 * @since 2018-09-27
 */
@Service
public class ReceiptAffirmDtlServiceImpl extends ServiceImpl<ReceiptAffirmDtlMapper, ReceiptAffirmDtl> implements ReceiptAffirmDtlService {
    @Autowired
    private InvoiceRegService invoiceRegService;

    /**
     * 根据条件获取收款确认明细
     *
     * @param condition
     * @return
     */
    @Override
    public List<ReceiptAffirmDtl> selectReceiptAffirmDtlList(Map<String, Object> condition) {
        return null;
    }

    /**
     * 根据头id获取收款确认明细
     *
     * @param headId
     * @return
     */
    @Override
    public List<ReceiptAffirmDtl> selectReceiptAffirmDtlByHeadId(Long headId) {
        EntityWrapper<ReceiptAffirmDtl> receiptAffirmDtlEntityWrapper = new EntityWrapper<>();
        receiptAffirmDtlEntityWrapper.eq("head_id", headId);
        return this.selectList(receiptAffirmDtlEntityWrapper);
    }

    @Override
    public List<ReceiptAffirmDtlAndInvoiceVO> bindInvoiceReceiptAffirm(List<ReceiptAffirmDtl> receiptAffirmDtls) {
        List<ReceiptAffirmDtlAndInvoiceVO> receiptAffirmDtlAndInvoiceVOS = new ArrayList<>();

        for (ReceiptAffirmDtl receiptAffirmDtl :
                receiptAffirmDtls) {
            ReceiptAffirmDtlAndInvoiceVO receiptAffirmDtlAndInvoiceVO = new ReceiptAffirmDtlAndInvoiceVO();
            BeanUtils.copyProperties(receiptAffirmDtl, receiptAffirmDtlAndInvoiceVO);
            InvoiceReg invoiceReg = invoiceRegService.selectById(receiptAffirmDtlAndInvoiceVO.getInvoiceId());
            if (invoiceReg != null) {
                receiptAffirmDtlAndInvoiceVO.setInvoiceDate(invoiceReg.getInvoiceDate());
                receiptAffirmDtlAndInvoiceVO.setInvoiceLackSum(invoiceReg.getInvoiceLackSum());
            }
            receiptAffirmDtlAndInvoiceVOS.add(receiptAffirmDtlAndInvoiceVO);
        }
        return receiptAffirmDtlAndInvoiceVOS;
    }
}
